// pages/user/registerService/index.js
import requestUrls from '../../../requestUrl.js'
import requestUtil from '../../../baseRequest.js'
import { $Message } from '../../../components/base/index.js'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        uploadIdCard: {
            count: 2,
            uploadComplete: 'uploadIdCardComplete',
            attentionText: '1.身份证照片需上传正反两面',
            uploadUrl: requestUrls.baseUrl + requestUrls.uploadUrl.idCard,
            isUpload: false
        },
    },
    /**完成身份证照片上传回掉*/
    uploadIdCardComplete({ detail }) {
        console.log('完成身份证照片上传回掉', detail)
        wx.hideLoading()
        this.data.formTemp.idCards = detail.map((item) => {
            return {
                'url': item.url,
                'id': item.id
            }
        })
        this.handleRegisterService(this.data.formTemp)
    },
    /**请求提交认证信息 */
    handleRegisterService(param) {
        param.companyRegion = param.companyRegion.join(',')
        param.openid = this.data.openid
        let url = requestUrls.service.registerService
        if (this.data.type === '2') {
            url = requestUrls.service.updateServiceInfo
        }
        requestUtil.postRequest(url, param, (res) => {
            getApp().updateGlobalUserAgencyStatus(0)
            wx.showModal({
                title: '温馨提示',
                content: '公益推广认证信息提交成功，请耐心等待审核',
                showCancel: false,
                success: res => {
                    if (res.confirm) wx.navigateBack()
                }
            })
        }, () => {})
    },
    /**显示错误信息 */
    showErrorMessage(msg) {
        $Message({
            content: msg,
            type: 'error'
        })
    },
    /**提交表单信息 */
    submitForm({ detail }) {
        console.log(detail.value)
        if (this.checkFormValue(detail.value)) {
            this.data.formTemp = detail.value
            this.setData({
                ['uploadIdCard.isUpload']: true,
            })
            wx.showLoading({
                title: '图片上传中'
            })
        }
    },
    /**检验表单信息 */
    checkFormValue(formData) {
        if (!formData.agentName) {
            this.showErrorMessage('真实姓名填写有误')
            return false
        } else if (!formData.companyRegion) {
            this.showErrorMessage('请选择您所在地区域信息')
            return false
        } else if (!formData.idCards || formData.idCards.length != 2) {
            this.showErrorMessage('身份证上传图片数量有误')
            return false
        }
        return true
    },

    /**获取代理人认证信息 */
    handleGetServiceInfo() {
        let param = { openid: this.data.openid }

        requestUtil.postRequest(requestUrls.service.getServiceInfo, param, (res) => {
            res.companyRegion = res.companyRegion ? res.companyRegion.split(',') : null
                // let idCards = []
                // if (res.idCards) idCards.push(res.idCards)
                // res.idCards = idCards
            res.idCards = res.idCards || []
            this.setData({
                serviceInfo: res
            })
        }, () => {})
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.data.openid = getApp().getUserOpenid()
        this.data.type = options.type || '1'
        this.handleGetServiceInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})